home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / FileFormat.C < prev    next >
Text File  |  1996-07-05  |  2KB  |  75 lines

  1. //    ============================================================================
  2. //    Fileformat.C                                                    80 columns
  3. //    Reece Hart    (reece@ibc.wustl.edu)                                tab=4 spaces
  4. //    Washington University School of Medicine, St. Louis, Missouri
  5. //    This source is hereby released to the public domain.  Bug reports, code
  6. //    contributions, and suggestions are appreciated (to email address above).
  7. //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  8. //    Please see FileFormat.H for a description of this source and
  9. //    acknowledgements.
  10. //
  11. //    I've used C-style file i/o for ease of translation from ted, and because
  12. //    some of the operations here are more easily done with stdio calls than with
  13. //    stream routines.
  14. //    ========================================|===================================
  15.  
  16. #include    <stddef.h>
  17. #include    <stdio.h>
  18. #include    <string.h>
  19. #include    "FileFormat.H"
  20. #define _H_STORAGE  // dgg
  21. #include    "RInclude.H"
  22.  
  23. format_t
  24. FileFormat(const char* filename)
  25.     {
  26.     const uint NUM_MAGICS = 4;                // number of magics we know about
  27.     const uint MAX_COOKIE = 5;                // max length of a magic cookie
  28.     const struct                            // The magics[] is a list of magic
  29.         {                                    // cookies and their file offsets
  30.         format_t type;                        // The scope of magics[] is limited
  31.         size_t    offset;                        // to this function to preserve
  32.         char*    string;                        // global namespace.
  33.         }
  34.         magics[] =
  35.         {
  36.             { ABI , 0,   "ABIF" } ,
  37.             { ALF , 518, "ALF " } ,
  38.             { SCF , 0,   ".scf" } ,
  39.             { SCF , 0,   "\234\330\300\000" }    // Amersham variant
  40.         };
  41.  
  42.  
  43.     FILE*    fp;
  44.     char    buf[MAX_COOKIE];
  45.     uint    i;
  46.     size_t    len;
  47.  
  48.     fp = fopen(filename, "rb");
  49.     if (!fp)
  50.         return FFerr;
  51.  
  52.     for (i = 0 ; i < NUM_MAGICS ; i++)
  53.         {
  54.         len = strlen(magics[i].string);
  55.  
  56.         if ( (fseek(fp,magics[i].offset,0) != 0) ||
  57.              (fread(buf,len,sizeof(char),fp) != 1) )
  58.             {
  59.             fclose(fp);
  60.             return unknown;
  61.             }
  62.         else
  63.             if (strncmp(buf,magics[i].string,len)==0)
  64.                 // magic cookie found
  65.                 {
  66.                 fclose(fp);
  67.                 return magics[i].type;
  68.                 }
  69.         }
  70.  
  71.     // we've not found our magic cookie... this file format is unknown
  72.     fclose(fp);
  73.     return unknown;
  74.     }
  75.